package com.boot.cluster.worker.splitter;

import com.boot.cluster.worker.bean.TaskSplitterInfo;

import java.util.ArrayList;
import java.util.List;


public class PageSplitter implements Splitter{
	/***
	 * 总数
	 */
	private Long total;
	/***
	 * 每页数量
	 */
	private int pageSize;

	public PageSplitter(Long total,int pageSize) {
		if(total==null) {
			throw new IllegalArgumentException("total is null");
		}
		if(pageSize==0) {
			throw new IllegalArgumentException("pageSize can't be zero");
		}
		this.pageSize=pageSize;
		this.total=total;
	}

	@Override
	public List<TaskSplitterInfo> excute() {
		List<TaskSplitterInfo> list = new ArrayList<>();
		Long div=this.total/this.pageSize;
		Long pageTotal = (this.total%this.pageSize)!=0?(div+1):div;
		for(Integer i =1;i<=pageTotal;i++) {
			Integer offset= ((i-1)*this.pageSize);
			list.add(new TaskSplitterInfo(this.pageSize,i,offset));
		}
		return list;
	}
}
